/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package aeducommon;

import java.util.*;

/**
 *
 * @author Adrian Hostettler
 */
public class Player {

    private String name;
    private int id;
    private EnumMap<CardColor, Integer> amountOfCards;

    public Player(String name, int id) {
        this.name = name;
        this.id = id;
        amountOfCards = new EnumMap<CardColor, Integer>(CardColor.class);
        for (CardColor cardColor : CardColor.values()) {
            amountOfCards.put(cardColor, 0);
        }
        Random generator = new Random();
        CardColor randomEnum = CardColor.values()[generator.nextInt(CardColor.values().length)];
        amountOfCards.put(randomEnum, 1);
    }

    public int getAmountOfCardColor(CardColor color) {
        return amountOfCards.get(color);
    }

    public int getTotalScore() {
        int totalScore = 0;
        ArrayList<Integer> amountCounts = new ArrayList<Integer>(7);
        for (CardColor cardColor : CardColor.values()) {
            amountCounts.add(amountOfCards.get(cardColor));
        }
        Collections.sort(amountCounts);
        for (int i = 0; i < 7; i++) {
            if (i < 4) {
                totalScore -= getPointsForCards(amountCounts.get(i));
            } else {
                totalScore += getPointsForCards(amountCounts.get(i));
            }
        }
        return totalScore;
    }

    public void takeUpCards(ArrayList<Card> cards) {
        for (Card card : cards) {
            int actualAmount = getAmountOfCardColor(card.getColor());
            amountOfCards.put(card.getColor(), actualAmount + 1);
        }
    }

    private int getPointsForCards(Integer i) {
        switch (i) {
            case 0:
                return 0;
            case 1:
                return 1;
            case 2:
                return 3;
            case 3:
                return 6;
            case 4:
                return 10;
            case 5:
                return 15;
            default:
                return 21;
        }
    }

    public String getName() {
        return this.name;
    }

    public int getId() {
        return id;
    }
}
